﻿using MesServices.CTmisStudioPublicService;
using MesServices.MESHelper;
using MesServices.ProcessService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static MesServices.MesName;
using static System.Collections.Specialized.BitVector32;

namespace MesServices
{
    
    public class OntMes:IMes_PassStation
    {
        private bool False(ref string errMsg,string Info)
        {
            errMsg = Info;
            return false;
        }

        /// <summary>
        /// 获取计划号
        /// </summary>
        /// <param name="SN">过站条码</param>
        /// <param name="planName">计划号</param>
        /// <returns></returns>
        public bool GetPlanName(string SN, ref string planName, ref string errMsg)
        {
            MESHelperSoapClient mesHelperSoapClient = new MESHelperSoapClient();
            DataTable dataTable = mesHelperSoapClient.GetBarcodesByCHSN(SN, out errMsg);
            if (dataTable == null || !string.IsNullOrEmpty(errMsg))
            {
                return False(ref errMsg, $"根据条码获取计划号错误:{errMsg}");
            }
            //planName = tmisStudioP.GetTaskNumByProIdOrPcba(guid);
            planName = dataTable.Rows[0]["TASKSCODE"].ToString();
            if (string.IsNullOrEmpty(planName))
            {
                return False(ref errMsg, $"根据条码获取计划号错误:TASKCODE为空");
            }
            return true;
        }

        /// <summary>
        /// 站前检查
        /// </summary>
        /// <param name="input">Line、Task、SN、Station、UserName、Platform、Equipment</param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public bool BeforeTest(Dictionary<MesName,string> input, ref string errMsg)
        {
            ProcessServiceSoapClient processServiceS = new ProcessServiceSoapClient();
            bool res = true;
            try
            {
                string planName = "";
                res = GetPlanName(input[SN], ref planName, ref errMsg);
                input[Plan] = planName;
                //planName = tmisStudioP.GetTaskNumByProIdOrPcba(mac);
                if (!res)
                {
                    return false;
                }
                string beforetestXml = $@"<?xml version=""1.0\"" encoding=""utf-8\""?>
<BeforeTest>
    <Line>{input[Line]}</Line>
    <TaskNo>{input[Plan]}</TaskNo>
    <ProSN>{input[SN]}</ProSN>
    <AtStation>{input[Station]}</AtStation>
    <OperatorID>{input[UserName]}</OperatorID>
    <PlatformID>{input[Platform]}</PlatformID>
    <EquipmentID>{input[Equipment]}</EquipmentID>
</BeforeTest>";
                errMsg = processServiceS.BeforeTest(beforetestXml);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    var _xd = XDocument.Parse(errMsg);
                    string result = _xd.Element("Result").Element("ResultNo").Value;
                    errMsg = _xd.Element("Result").Element("ResultMsg").Value;
                    int ret1 = int.Parse(result);
                    if (ret1 > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                return False(ref errMsg, $"站前检测失败:{ex.Message}");
            }
        }
        /// <summary>
        /// 过站
        /// </summary>
        /// <param name="input">Line、SN、Station、UserName、Result</param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public bool AfterTest(Dictionary<MesName, string> input, ref string errMsg)
        {
            ProcessServiceSoapClient processServiceS = new ProcessServiceSoapClient();
            bool res = true;
            try
            {
                string planName = "";
                res = GetPlanName(input[SN], ref planName, ref errMsg);
                input[Plan] = planName;
                //planName = tmisStudioP.GetTaskNumByProIdOrPcba(mac);
                if (!res)
                {
                    return false;
                }
                //Result= 0成功:1失败
                string aftertestXml = $@"<?xml version=""1.0\"" encoding=""utf-8\""?>
<AfterTest>
    <Line>{input[Line]}</Line>
    <TaskNo>{input[Plan]}</TaskNo>
    <ProSN>{input[SN]}</ProSN>
    <AtStation>{input[Station]}</AtStation>
    <OperatorID>{input[UserName]}</OperatorID>
    <PlatformID>{input[Platform]}</PlatformID>
    <Result>{(input[Result].Trim().ToLower()=="true"?"0":"1")}</Result>
    <EquipmentID>{input[Equipment]}</EquipmentID>
</AfterTest>";
                errMsg = processServiceS.AfterTest(aftertestXml);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    var _xd = XDocument.Parse(errMsg);
                    string retstr = _xd.Element("Result").Element("ResultNo").Value;
                    errMsg = _xd.Element("Result").Element("ResultMsg").Value;
                    if ("null" == errMsg.Trim().ToLower())
                    {
                        errMsg = "";
                    }
                    int ret1 = int.Parse(retstr);
                    if (ret1 > 0)
                    {
                        res = true;
                        return res;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                return False(ref errMsg, $"过站失败:{ex.Message}");
            }
        }
        /// <summary>
        /// 上传测试错误信息
        /// </summary>
        /// <param name="input">SN、Station、UserName、Result、ErrorMessage</param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public bool UploadErrorInfo(Dictionary<MesName, string> input, ref string errMsg)
        {
            TmisStudioPublicServiceClient tmisStudioP = new TmisStudioPublicServiceClient();
            bool res = true;
            try
            {
                res = tmisStudioP.SubmitTestResultForTps(input[SN], Convert.ToBoolean(input[Result]), input[ErrorMessage], input[Station], input[UserName], ref errMsg);
            }
            catch (Exception ex)
            {
                return False(ref errMsg, $"上传测试错误信息失败:{ex.Message}");
            }
            return res;
        }
    }
}
